SOC Tutor

Simple Character Device Driver Code Explanation

 

 

First, we include the packages to be used.

  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/fs.h>
  4. #include <linux/uaccess.h>
  5. #include <asm/errno.h>
  6. #include <linux/io.h>
  7. #include <linux/of.h>
  8. #include <linux/of_device.h>

linux/kernel.h shows that we are doing kernel work, and allows for the printk() command to be used, which prints messages to the kernel log. printk() is used mainly as a logging mechanism for the kernel, which logs information and gives warnings.

linux/module.h shows that we are specifically working on a module with regard to kernel work. Modules are code segments that can be loaded and unloaded to the kernel on demand.

linux/fs.h defines the file structure of the device via the kernel. It will be used later in line 148 to add the driver to the system via the register_chrdev command.

linux/uaccess.h allows for the usage of the get_user and put_user commands. This includes the copy_to_user command in line 86 and the copy_from_user command in line 101.

asm/errno.h is necessary in coding the Linux Kernel.

linux/io.h and linux/of.h are C wrappers for input/output interfaces and macros for many other useful functions.

linux/of_device.h adds an optional input_id parameter, leaving current functionality unchanged.

  1. /*
  2. 0x43c0_0000 + 4 = value
  3. 0x43c0_0000 + 8 = direction
  4. */
  5. #define DEVICE_NAME “fii-module”
  6. #define CLASS_NAME “fii-module_class”
  7. static int major;
  8. static struct class *swled_class;
  9. static unsigned int *gpio_data;
  10. static unsigned int *gpio_dir ;

The above segment simply does some definitions.

  1. static int swled_open(struct inode *node,struct file *filp){
  2. unsigned int gpio_base = (0x43c00000);
  3. gpio_data = ioremap(gpio_base,8);
  4. if(gpio_data){
  5. printk(“kernel: ioremap(0x%08x)=0x%08x \n”,gpio_base,gpio_data);
  6. }
  7. else{
  8. return -EINVAL;
  9. }
  10. gpio_dir = gpio_data + 1;
  11. printk(“kernel: gpio_data address = 0x%08x \n”, gpio_data);
  12. printk(“kernel: gpio_dir address = 0x%08x \n”, gpio_dir);
  13. return 0;
  14. }

Because the swled_open function controls a section of buffer memory, we will need to use the read and write functions to modify the buffer memory. This section serves to open the device.

ioremap maps the physical address of an input/output device to the kernel virtual address. Where the input is (physical_address, size). We can see that the physical address is gpio_base, and the size is 8.

*Note that Linux remaps the physical addresses of devices for easier management.

Because gpio_data is a pointer to an address, it will mean it loaded incorrectly if it has a value of 0. Thus, it will proceed to throw -EINVAL.

Note that -EINVAL is a macro for “Invalid Argument”.

The next two printk lines help log the data and directory.

  1. int swled_release(struct inode *inode, struct file *filp)
  2. {
  3. // MOD_DEC_USE_COUNT;
  4. iounmap(gpio_data);
  5. return 0;
  6. }

The above section serves to close and release the device. iounmap is the opposite of ioremap. While ioremap maps physical addresses, iounmap destroys it.

  1. static ssize_t swled_read(struct file *filp, char *buf, size_t size, loff_t *offset)
  2. {
  3. unsigned char val[32];
  4. unsigned int temp;
  5. unsigned int *addr_p;
  6. int i = 0, cnt;
  7. printk(“kernel: swled_read start…. size = %d\n”, size);
  8. cnt = size;
  9. addr_p = gpio_data;
  10. temp = *addr_p ;
  11. for(i = 0; i < cnt/4; i++ )
  12. {
  13. val[i*4 + 3] = temp & 0xff;
  14. val[i*4 + 2] = (temp >> 8) & 0xff;
  15. val[i*4 + 1] = (temp >> 16) & 0xff;
  16. val[i*4 + 0] = (temp >> 24) & 0xff;
  17. addr_p ++;
  18. temp = *addr_p;
  19. }
  20. if(i % 4 == 1)
  21. {
  22. val[i*4 + 0] = temp & 0xff;
  23. }
  24. if(i % 4 == 2)
  25. {
  26. val[i*4 + 1] = temp & 0xff;
  27. val[i*4 + 0] = (temp >> 8) & 0xff;
  28. }
  29. if(i % 4 == 3)
  30. {
  31. val[i*4 + 2] = temp & 0xff;
  32. val[i*4 + 1] = (temp >> 8) & 0xff;
  33. val[i*4 + 0] = (temp >> 16) & 0xff;
  34. }
  35. copy_to_user(buf, val, cnt);
  36. return cnt;
  37. }

The above section serves to read from the device. It is taking the information stored in temp, which is the address of gpio_data, and storing it in the val array. Note that the copy_to_user function at the end has the parameters (destination, source, size). Where the function will copy size number of bytes pointed at by source, which much exist in kernel-space, to destination, which must exist in user-space.

The main part of this section is lines 60 to 84. It breaks up the data into 8-bit chunks, and stores the chunks in reverse order, which allows for it to be in normal order when it is eventually read. Note that & 0xff serves as a mask which only leaves the right-most 8 bits. If the size is not a perfect multiple of 4, it will simply append the extras to the end.

  1. static ssize_t swled_write(struct file * filp, const char __user *buf, size_t size, loff_t * offset) {
  2. unsigned char val[32];
  3. unsigned int temp = 0;
  4. unsigned int * addr_p;
  5. int i,cnt;
  6. memset(val,0,32);
  7. addr_p = gpio_data;
  8. printk(“kernel: swled_write start…. size = %d\n”, size);
  9. copy_from_user(&val, buf, size);
  10. cnt = size – 1;
  11. printk(“kernel: val[0] = 0x%08x \n”, val[0]);
  12. if(val[0] == ‘w’)
  13. {
  14. temp = val[2];
  15. temp = temp << 8 | val[3];
  16. temp = temp << 8 | val[4];
  17. temp = temp << 8 | val[5];
  18. *addr_p = temp;
  19. printk(“kernel: gpio_data = 0x%08x \n”, temp);
  20. }
  21. else if(val[0] == ‘d’)
  22. {
  23. addr_p ++;
  24. temp = val[2];
  25. temp = temp << 8 | val[3];
  26. temp = temp << 8 | val[4];
  27. temp = temp << 8 | val[5];
  28. *addr_p = temp;
  29. printk(“kernel: gpio_dir = 0x%08x \n”, temp);
  30. }
  31. else
  32. {
  33. printk(“kernel: invalid parameter \n”);
  34. }
  35. return size;
  36. }

The above section focuses on writing to the device. Making use of the copy_from_user command, which also has the parameters (destination, source, size). Where the function will copy size number of bytes pointed at by source, which much exist in user-space, to destination, which must exist in kernel-space.

The section also deals with taking in the inputs from the application, in the case of the ‘dir’ ‘wr’ and ‘rd’ commands. It takes in the first character to determine which command it is and prepares the data, as well as writes data to the log file accordingly.

  1. static struct file_operations swled_oprs = {
  2. .owner = THIS_MODULE,
  3. .open = swled_open,
  4. .write = swled_write,
  5. .read = swled_read,
  6. .release= swled_release,
  7. };

The above section deals with the file_operations structure, which is defined in linux/fs.h, and it holds various pointers to functions defined by the driver in order to perform operations on the device.

  1. static int swled_init(void){ /* register device */
  2. /*
  3. *
  4. * int register_chrdev (unsigned int major,
  5. * const char *name,
  6. * struct file_operations *fops);
  7. *
  8. * major => 0, automatic mode; others major device ID
  9. */
  10. major=register_chrdev(0, DEVICE_NAME, &swled_oprs);
  11. if (major < 0) {
  12. printk (“Registering the character device failed with %d\n”, major);
  13. return major;
  14. }
  15. swled_class = class_create(THIS_MODULE, CLASS_NAME);
  16. device_create(swled_class, NULL, MKDEV(major,0), NULL, DEVICE_NAME);
  17. return 0;
  18. }

The above section deals with driver entry functions. Char devices are accessed through device files, which is typically located in the /dev folder.

Registering the device with the kernel adds the driver to the system. Note that this is done using the register_chrdev function, which is defined in linux/fs.h.

Where unsigned int major is the major number to be requested, const char *name is the name of the device which will appear in /proc/devices and struct file_operations *fops is a pointer to the file_operations table for the driver. Note that if the function returns a negative value, it means that the registration failed.

  1. static void swled_exit(void){
  2. unregister_chrdev(major, DEVICE_NAME);
  3. device_destroy(swled_class,MKDEV(major,0));
  4. class_destroy(swled_class);
  5. };
  6. module_init(swled_init);
  7. module_exit(swled_exit);
  8. MODULE_LICENSE(“GPL”);

Here, the driver exit function is defined above. The device is removed from the system via unregister_chrdev and device_destroy. While class_destroy destroys the struct class structure along with pointers.

module_init() defines the function to be called during module insertion or upon boot. In this case the function is swled_init.

module_exit() defines the function to be called during module removal. Everything must be cleaned up when it returns. In this case the function is swled_exit.

MODULE_LICENSE(“GPL”) is used to suppress the warning saying the code is not open source.

 

Copyright Notice:

© 2020 Fraser Innovation Inc ALL RIGHTS RESERVED

Without written permission of Fraser Innovation Inc, no unit or individual may extract or modify part of or all the contents of this manual. Offenders will be held liable for their legal responsibility.

Thank you for purchasing the FPGA development board. Please read the manual carefully before using the product and make sure that you know how to use the product correctly. Improper operation may damage the development board. This manual is constantly updated, and it is recommended that you download the latest version when using.

Official Shopping Website:

https://fpgamarketing.com/

Visited 1 times, 1 visit(s) today

Related posts